<template>
  <div class="rank-container">
    <div class="rank-header">
      <div class="title-section">
        <h2>积分排行榜</h2>
        <div class="subtitle">每日更新，展示最新积分排名</div>
      </div>
      <el-button @click="$router.push('/home')" class="back-btn">
        <el-icon><Back /></el-icon>
        返回首页
      </el-button>
    </div>

    <div class="rank-content">
      <template v-if="rankList.length">
        <!-- 前三名特殊展示 -->
        <div class="top-three">
          <!-- 第二名 -->
          <div v-if="rankList[1]" class="top-item second-place">
            <div class="crown silver">2</div>
            <el-avatar :size="70" :src="rankList[1].avatar" />
            <div class="user-info">
              <div class="username">{{ rankList[1].nickname || rankList[1].username }}</div>
              <div class="points">{{ rankList[1].credit || 0 }}分</div>
            </div>
          </div>
          
          <!-- 第一名 -->
          <div v-if="rankList[0]" class="top-item first-place">
            <div class="crown gold">1</div>
            <el-avatar :size="90" :src="rankList[0].avatar" />
            <div class="user-info">
              <div class="username">{{ rankList[0].nickname || rankList[0].username }}</div>
              <div class="points">{{ rankList[0].credit || 0 }}分</div>
            </div>
          </div>
          
          <!-- 第三名 -->
          <div v-if="rankList[2]" class="top-item third-place">
            <div class="crown bronze">3</div>
            <el-avatar :size="70" :src="rankList[2].avatar" />
            <div class="user-info">
              <div class="username">{{ rankList[2].nickname || rankList[2].username }}</div>
              <div class="points">{{ rankList[2].credit || 0 }}分</div>
            </div>
          </div>
        </div>

        <!-- 其他排名 -->
        <el-card class="rank-list">
          <div v-for="(user, index) in rankList.slice(3)" :key="user.id" class="rank-item">
            <div class="rank-info">
              <div class="rank-number">{{ index + 4 }}</div>
              <el-avatar :size="50" :src="user.avatar" />
              <div class="user-info">
                <div class="username">{{ user.nickname || user.username }}</div>
                <div class="points">{{ user.credit || 0 }}分</div>
              </div>
            </div>
          </div>
        </el-card>
      </template>
      <el-empty v-else description="暂无排行数据" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Back } from '@element-plus/icons-vue'
import axios from 'axios'

const request = axios.create({
  baseURL: '/api',
  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

const rankList = ref([])

// 获取积分排行榜
const fetchRankList = async () => {
  try {
    const response = await request.get('/v1/medical/credit/query_user_credit_rank')
    if (response.data.code === '0000') {
      rankList.value = response.data.data
    }
  } catch (error) {
    console.error('获取排行榜失败:', error)
  }
}

onMounted(() => {
  fetchRankList()
})
</script>

<style lang="scss" scoped>
.rank-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;

  .rank-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 40px;

    .title-section {
      h2 {
        margin: 0;
        color: #2c3e50;
        font-size: 28px;
      }

      .subtitle {
        margin-top: 5px;
        color: #909399;
        font-size: 14px;
      }
    }

    .back-btn {
      transition: all 0.3s;

      &:hover {
        transform: translateX(-5px);
      }

      .el-icon {
        margin-right: 5px;
      }
    }
  }

  .rank-content {
    .top-three {
      display: flex;
      justify-content: center;
      align-items: flex-end;
      margin-bottom: 40px;
      padding: 20px;
      background: linear-gradient(to bottom, #f6f8fa, #fff);
      border-radius: 12px;

      .top-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 20px;
        transition: all 0.3s;
        position: relative;

        &:hover {
          transform: translateY(-10px);
        }

        .crown {
          position: absolute;
          top: -15px;
          width: 30px;
          height: 30px;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          color: white;
          font-weight: bold;
          font-size: 16px;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

          &.gold {
            background: linear-gradient(135deg, #ffd700, #ffa500);
          }

          &.silver {
            background: linear-gradient(135deg, #c0c0c0, #a9a9a9);
          }

          &.bronze {
            background: linear-gradient(135deg, #cd7f32, #a0522d);
          }
        }

        .el-avatar {
          border: 3px solid white;
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
          margin: 10px 0;
        }

        .user-info {
          text-align: center;
          margin-top: 10px;

          .username {
            font-size: 16px;
            color: #2c3e50;
            font-weight: bold;
            margin-bottom: 5px;
          }

          .points {
            font-size: 14px;
            background: linear-gradient(45deg, #ff6b6b, #f56c6c);
            color: white;
            padding: 2px 10px;
            border-radius: 12px;
            box-shadow: 0 2px 6px rgba(245, 108, 108, 0.2);
          }
        }

        &.first-place {
          z-index: 3;
          margin: 0 20px;
          
          .el-avatar {
            border-color: #ffd700;
          }
        }

        &.second-place {
          z-index: 2;
          
          .el-avatar {
            border-color: #c0c0c0;
          }
        }

        &.third-place {
          z-index: 1;
          
          .el-avatar {
            border-color: #cd7f32;
          }
        }
      }
    }

    .rank-list {
      border-radius: 12px;
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);

      .rank-item {
        padding: 15px 20px;
        border-bottom: 1px solid #f0f0f0;
        transition: all 0.3s;

        &:hover {
          background-color: #f6f8fa;
        }

        &:last-child {
          border-bottom: none;
        }

        .rank-info {
          display: flex;
          align-items: center;
          gap: 20px;

          .rank-number {
            font-size: 20px;
            font-weight: bold;
            color: #909399;
            width: 40px;
            text-align: center;
          }

          .el-avatar {
            border: 2px solid #fff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
          }

          .user-info {
            flex: 1;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .username {
              font-size: 16px;
              color: #2c3e50;
            }

            .points {
              font-size: 14px;
              color: #f56c6c;
              font-weight: bold;
              background-color: #fff0f0;
              padding: 2px 10px;
              border-radius: 12px;
            }
          }
        }
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .rank-container {
    padding: 10px;

    .rank-header {
      flex-direction: column;
      gap: 15px;
      text-align: center;

      .back-btn {
        width: 100%;
      }
    }

    .rank-content {
      .top-three {
        flex-direction: column;
        align-items: center;
        gap: 30px;
        padding: 40px 20px;

        .top-item {
          &.first-place {
            order: 1;
          }
          &.second-place {
            order: 2;
          }
          &.third-place {
            order: 3;
          }
        }
      }

      .rank-list {
        .rank-item {
          .rank-info {
            .user-info {
              flex-direction: column;
              align-items: flex-start;
              gap: 5px;
            }
          }
        }
      }
    }
  }
}
</style> 